package com.five.controller.backstage;

import com.five.dao.backstage.OrderManageDao;
import com.five.dao.backstage.QianTaiDao;
import com.five.dao.reception.OrderDao;
import com.five.dao.reception.ShowTypeDao;
import com.five.entity.backstage.TChange;
import com.five.entity.backstage.TTuifang;
import com.five.entity.backstage.TXufang;
import com.five.entity.reception.Order;
import com.five.util.LayTable;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 王登峰
 * @Description:
 * @Date: 2020/12/2 14:26
 */
@RestController
@RequestMapping("/qiantai")
public class QianTaiController {
    @Autowired
    QianTaiDao qianTaiDao;
    @Autowired
    OrderDao orderDao;
    @Autowired
    ShowTypeDao typeDao;
    @RequestMapping("/selectByTID")
    @ResponseBody
    public List<Map> selectAll(){
        return qianTaiDao.selectAllRoom();
    }

    @RequestMapping("/addtuifang")
    @ResponseBody
    public String  addTuiFang(TTuifang tTuifang,Integer tuicount){
        System.out.println("tuicount"+tuicount);
        int i = 0;
        if(tTuifang.getRuzhuid() != null && tTuifang.getRuzhuid() !=""){
          i  = qianTaiDao.addTuiFang(tTuifang);
        }else {
            for(int j = 0; j< tuicount;j++){
                i=qianTaiDao.addTuiFang(tTuifang);
            }
        }
        if(i > 0){
            int j = qianTaiDao.changeStatus(tTuifang.getRoomcode());
            int count = qianTaiDao.countByOrder(tTuifang.getOrderid());
            int tuifang = qianTaiDao.orderTuiFang(tTuifang.getOrderid());
            if(tuifang == count){
                qianTaiDao.changeOrderStatus(tTuifang.getOrderid());
            }
            if(j > 0)
            return tTuifang.getRoomcode();
        }
       return "0";
    }
    //打扫后改变房间的状态
    @RequestMapping("/dasao")
    @ResponseBody
    public int  daSao(String roomcode){
       return qianTaiDao.changeStatus2(roomcode);
    }
    //改变房间时先查出该类型空闲的房间
    @RequestMapping("/selectbyky")
    @ResponseBody
    public List<Map> selectByKY(Integer typeid){
        return qianTaiDao.selectByKY(typeid);
    }
    @RequestMapping("/changeroom")
    @ResponseBody
    public int changeRoom(TChange tChange){

        int i = qianTaiDao.addChangeRoom(tChange);
        if(i > 0){
            qianTaiDao.changeStatus(tChange.getOldroomid());
            qianTaiDao.changeStatus3(tChange.getNewroomid());
            qianTaiDao.changeRuzhuDetail(tChange.getNewroomid(),tChange.getOldroomid());
            qianTaiDao.changeruzhustatus(tChange.getRuzhuid());
        }
        return 1;
    }
    @RequestMapping("/selectchangeroom")
    @ResponseBody
    public  List<Map> selectChangeRoom(String ruzhuid){
        System.out.println(ruzhuid);
        return qianTaiDao.selectChangeRoom(ruzhuid);
    }
    @RequestMapping("/changecount")
    @ResponseBody
    public  int Changecount(String ruzhuid){
        return  qianTaiDao.changCount(ruzhuid);
    }

    @RequestMapping("/orderstatus")
    @ResponseBody
    public  Map orderStatus(String orderid) {
        int fenpei = qianTaiDao.orderFenPei(orderid);
        int tuifang = qianTaiDao.orderTuiFang(orderid);
        Map map = new HashMap();
        map.put("feipei",fenpei);
        map.put("tuifang",tuifang);
        return map;
    }
    @RequestMapping("/tiqiantui")
    @ResponseBody
    public  List<Map> tiQianTUi(String orderid)  {
        return  qianTaiDao.selectTiQianTui(orderid);
    }
    @RequestMapping("/istiqian")
    @ResponseBody
    public  int isTiQian(String orderid)  {
        return  qianTaiDao.isTiQian(orderid);
    }
    @RequestMapping("/iszhugou")
    @ResponseBody
    public  int Zhugou(String orderid)  {
        int zhuGuo = qianTaiDao.isZhuGuo(orderid);
        if(zhuGuo > 0){
         return  qianTaiDao.selectZhuGou(orderid);
         //如果住过 改变order表里的iszhugou为1
        }else
          return -1;
    }
//    如果住过就改变状态
    @RequestMapping("/zhugou")
    @ResponseBody
    public  int selectZhugou(String orderid)  {
        return  qianTaiDao.selectZhuGou(orderid);
    }
    //    最晚的退房时间
    @RequestMapping("/selectzuiwan")
    @ResponseBody
    public  String selectZuiWan(String orderid)  {
        return  qianTaiDao.selectZuiWan(orderid);
    }

    @RequestMapping("/changeorderstatus")
    @ResponseBody
    public  int  changeOrderStatus(String orderid){
        System.out.println("所有订单都退完了");
        return qianTaiDao.changeOrderStatus(orderid);
    }

    @RequestMapping("/addxufang")
    @ResponseBody
    public  String addXufang(String orderid, Order order,TXufang tXufang){
        System.out.println(order);
        System.out.println(tXufang);
        //根据预定的入住时间和离开时间  获取这中间的日期
        orderDao.getDate(order);
        //根据插入的时间查询当前的房间
        List<Map> mapList=typeDao.getAllDate();
        System.out.println("mapList:"+mapList);
        //查询这个时间段之内每天预定的房间数  以及已满的房间数
        System.out.println("order="+order);
        Map allList=typeDao.getDateOrder(mapList,order.getRoomtype());
        System.out.println("allist"+allList);
        //如果预定的房间数大于这个剩余的房间数的最小值 就 不能预定成功
        List<Map> mapsDate  = (List<Map>)allList.get("srDate");//[{date1:1},{date2:2}]
        boolean f=true;
        System.out.println("mapsDate"+mapsDate);
        for (Map map : mapsDate) {
//            map里面存放的格式是  时间:数量（剩余的房间的数量）
            for (Object o : map.keySet()) {
                //o是时间
                int synum = Integer.parseInt(map.get(o)+"");
                //对比一下当前预定的数量是否>剩余的数量  如果大于就直接return 0 （不能预定）
                if(1 > synum){
                    //return "0";
                    f=false;//不能预定了
                }
            }
        }
        System.out.println(f);
        if(f){
           qianTaiDao.addXuFang(tXufang);
           qianTaiDao.changeruzhustatus1(tXufang.getRuzhuid());
            return "1";
        }else{
            //不能预定了
            return "0";
        }

    }
    @RequestMapping("/selectxufang")
    @ResponseBody
    public  List<Map> selectXufang(String ruzhuid){
        return qianTaiDao.selectxufangfang(ruzhuid);
    }
    //取消订单时 改变订单状态为4
    @RequestMapping("/changeorder")
    @ResponseBody
    public  int changeOrder(String orderid){
        System.out.println(123);
        qianTaiDao.changequxiao(orderid);
        qianTaiDao.changeOrderStatus4(orderid);
        return 1;
    }
    //根据订单查找入住详情
    @RequestMapping("/selectruzhu")
    @ResponseBody
    public LayTable selectRuzhu(String orderid){
        System.out.println(orderid);
        List<Map> list = qianTaiDao.selectRuzhu(orderid);
        System.out.println(list);
        LayTable layTable = new LayTable(list.size(),list);
        return layTable;
    }
}
